import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution {
    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public int maxDepth1(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = maxDepth1(root.left);
        int rightHeight = maxDepth1(root.right);

        return (leftHeight > rightHeight) ?
                (leftHeight + 1) : (rightHeight + 1);
    }

    public boolean isBalanced1(TreeNode root) {
        if (root == null) {
            return true;
        }
        int hleft = maxDepth1(root.left);
        int hright = maxDepth1(root.right);
        return Math.abs(hleft - hright) <= 1 &&
                isBalanced1(root.left) && isBalanced1(root.right);
    }

    public int maxDepth2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = maxDepth2(root.left);
        if (leftHeight < 0) {
            return -1;
        }
        int rightHeight = maxDepth2(root.right);
        if(leftHeight>=0&&rightHeight>=0&&
        Math.abs(leftHeight-rightHeight)<=1){
            return Math.max(leftHeight, rightHeight) + 1;
        }else {
            return -1;
        }
    }
    public boolean isBalanced(TreeNode root) {
       return maxDepth2(root)>=0;
    }

    public boolean issymmetric(TreeNode l,TreeNode r) {
        if(l != null&&r == null || l == null && r != null) {
            return false;
        }
        if(l == null && r == null ) {
            return true;
        }
        if(l.val != r.val ) {
            return false;
        }
        return issymmetric(l.left,r.right) && issymmetric(l.right,r.left);
    }
    public boolean isSymmetric(TreeNode root) {
     if(root==null){
         return true;
     }
     return issymmetric(root.left,root.right);
    }
    // 后序遍历
    void postOrder(TreeNode root){
        if(root==null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.println(root.val+" ");
    }
    int  i;
    public  TreeNode createTree(String s){
        TreeNode root=null;
        char ch=s.charAt(i);
        if(ch!='#'){
           root=new TreeNode(ch);
           this.i++;
           root.left=createTree(s);
           root.right=createTree(s);
        }else {
            this.i++;
        }
        return root;
    }
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> lists = new ArrayList<>();
        if(root==null){
            return lists;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            ArrayList<Integer> tmp = new ArrayList<>();
            int size=queue.size();
            while (size>0){
                TreeNode cur=queue.poll();
                tmp.add(cur.val);
                size--;
                if(cur.left!=null){
                    queue.add(cur.left);
                }
                if(cur.right!=null){
                    queue.add(cur.right);
                }

            }
            lists.add(tmp);
        }
        return lists;
    }

    public boolean isCompleteTree(TreeNode root){
        if(root == null) {
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (queue.isEmpty()){
            TreeNode cur=queue.poll();
            if(cur!=null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else{
                break;
            }
        }
        while (!queue.isEmpty()) {
            TreeNode tmp = queue.poll();
            if(tmp != null) {
                return false;
            }
        }
        return true;
    }
}
